//
//  SessonContentMessage.m
//  BaiYing_Thinker
//
//  Created by 鹏 朱 on 15/10/22.
//  Copyright (c) 2015年 XKJH. All rights reserved.
//

#import "SessonContentMessage.h"
#import "NSDate+Addition.h"
#import "AppDelegate.h"

@implementation SessonContentMessage

-(id)initWithDictionary:(NSDictionary *)contentDic isMyself:(NSNumber *)isMyself{
    
    if (self = [super init]) {
        
        _isMyself = isMyself;
        if (contentDic[@"msg_id"]) {
            _msg_id = contentDic[@"msg_id"];
            _subject_id = contentDic[@"msg_id"];
        }
        if (contentDic[@"content"][@"type"]) {
            _msg_type = contentDic[@"content"][@"type"];
        }
        if (contentDic[@"content"][@"text"]) {
            _msg_content = contentDic[@"content"][@"text"];
        }
        if (contentDic[@"model"]) {
            _model = contentDic[@"model"];
            if ([_model isEqualToString:@"ASK"]) {
                _subject_content = _msg_content;
            }
        }
        if ([_msg_type isEqualToString:@"COMPONENT"]) {
            _subject_content = @"插件";
            _msg_content = @"插件";
        }
        if (contentDic[@"content"][@"height"]) {
            _content_height = contentDic[@"content"][@"height"];
        }
        if (contentDic[@"content"][@"width"]) {
            _content_width = contentDic[@"content"][@"width"];
        }
        if (contentDic[@"content"][@"timestamp"]) {
            _content_timestamp = contentDic[@"content"][@"timestamp"];
        }
        if (contentDic[@"clientid"]) {
            _sender = contentDic[@"clientid"];
            _session_id = contentDic[@"clientid"];
        }
        if (contentDic[@"toclientid"]) {
            _recevier = contentDic[@"toclientid"];
        }
        if (contentDic[@"content"][@"sys_code"]) {
            _sys_code = contentDic[@"content"][@"sys_code"];
        }
        if (contentDic[@"to_range"][@"rel"]) {
            _rel = contentDic[@"to_range"][@"rel"];
        }
        if (contentDic[@"to_range"][@"lbskm"]) {
            _lbskm = contentDic[@"to_range"][@"lbskm"];
        }
        if (contentDic[@"local_Path"]) {
            _local_Path = contentDic[@"local_Path"];
        }
        if (contentDic[@"content"][@"url"]) {
            _content_url = contentDic[@"content"][@"url"];
        }
        if (contentDic[@"content"][@"component"][@"name"]) {
            _componentName = contentDic[@"content"][@"component"][@"name"];
        }
        if (contentDic[@"content"][@"component"][@"url"]) {
            _componentUrl = contentDic[@"content"][@"component"][@"url"];
        }
        if (contentDic[@"content"][@"component"][@"rule"]) {
            _componentRule = contentDic[@"content"][@"component"][@"rule"];
        }
        if (contentDic[@"content"][@"component"][@"id"] && contentDic[@"content"][@"component"][@"version"]) {
            _local_Path = [NSString stringWithFormat:@"%@_%@",contentDic[@"content"][@"component"][@"id"],contentDic[@"content"][@"component"][@"version"]];
        }
        _create_time = [NSDate currentDateStringWithFormat:@"yyyy-MM-dd mm:HH:ss"];
        
        if ([_isMyself isEqual: @1]) {
            
            //发送消息的session_id
            _session_id = kLocalSessionId;
            
        } else {
            
            //接收消息的session_id
            if ([_model isEqualToString:@"SYSTEM"]) {
                _session_id = kLocalSessionId;
            } else {
                
                if (IsEmpty(_recevier)) {
                    
                    if ([_model isEqualToString:@"ASK"]) {
                        
                        //人问问题
                        _session_id = _sender;

                    } else {
                        //机器人发的应答消息
                        _session_id = kLocalSessionId;
                    }
                } else {
                    _session_id = _sender;
                }
            }
        }
    }
    
    return self;
}

@end
